window.onload = function (event) {
  event = event || window.event;
  // ***1.导航栏鼠标点击.active
  var all_nav_li = document.getElementsByName("nav");
  for (var i = 0; i < all_nav_li.length; i++) {
    (function (i) {
      all_nav_li[i].onclick = function (event) {
        event = event || window.event;
        for (j = 0; j < all_nav_li.length; j++) {
          all_nav_li[j].className = "li";
        }
        this.className += " nav_active";
      };
    })(i);
  }

  // ***2.tab鼠标点击.active，并筛选相应课程
  var all_tab = document.getElementsByName("tab");
  var all_tabBox = document.getElementsByClassName("WHOLE");
  var length = all_tabBox.length;
  for (var i = 0; i < all_tab.length; i++) {
    (function (i) {
      all_tab[i].onclick = function (event) {
        event = event || window.event;
        for (j = 0; j < all_tab.length; j++) {
          all_tab[j].className = "";
        }
        this.className += "tab_active";
        // console.log(all_tab[i].id);
        var box = document.getElementsByClassName(all_tab[i].id);
        for (k = 0; k < length; k++) {
          all_tabBox[k].style.display = "none";
        }
        for (l = 0; l < box.length; l++) {
          box[l].style.display = "block";
        }
      };
    })(i);
  }

  // ***3.入学年份onmouse事件
  var yearEnroll = document.getElementById("yearEnroll");
  var year = document.getElementById("year");
  var yearBox = document.getElementById("yearBox");
  var $yearBoxLis = $("#yearBox>li");
  onmouseImpl(yearEnroll, year, yearBox, $yearBoxLis);

  // ***4.学校onmouse事件
  var schoolEnroll = document.getElementById("schoolEnroll");
  var school = document.getElementById("school");
  var schoolBox = document.getElementById("schoolBox");
  var $schoolBoxLis = $(".schoolBoxLis>li");
  onmouseImpl(schoolEnroll, school, schoolBox, $schoolBoxLis);
  // 鼠标点击省市，切换相应container
  var $contents = $("#schoolBox_container>div");
  var $schoolBox_tabs = $("#schoolBox_tab>li");
  $("#schoolBox_tab>li").click(function (event) {
    event = event || window.event;
    // console.log($(this));
    var index = $(this).index();
    for (var i = 0; i < $schoolBox_tabs.length; i++) {
      $schoolBox_tabs[i].classList.remove("sthBoxLi_active");
    }
    $schoolBox_tabs[index].classList.add("sthBoxLi_active");
    $contents.css("display", "none");
    $contents[index].style.display = "block";
  });
  // !!!封装onmouse事件函数
  function onmouseImpl(sthEnroll, sth, sthBox, $sthBoxLis) {
    // 选择的值加强显示
    for (var i = 0; i < $sthBoxLis.length; i++) {
      if ($sthBoxLis[i].innerHTML == sth.innerHTML) {
        // console.log($sthBoxLis[i]);
        $sthBoxLis[i].classList.add("sthBoxLi_active");
      }
    }
    sthEnroll.onmouseover = function (event) {
      event = event || window.event;
      // console.log("移入了");
      sthBox.style.display = "block";
      sth.classList.add("sth_active");
      // 点击sth.innerHTML变化，并关闭
      for (var i = 0; i < $sthBoxLis.length; i++) {
        $sthBoxLis[i].onclick = function (event) {
          event = event || window.event;
          for (var j = 0; j < $sthBoxLis.length; j++) {
            if ($sthBoxLis[j].className == "sthBoxLi_active") {
              $sthBoxLis[j].classList.remove("sthBoxLi_active");
            }
          }
          sth.innerHTML = this.innerHTML;
          this.classList.add("sthBoxLi_active");
          sthBox.style.display = "none";
          sth.classList.remove("sth_active");
        };
      }
    };
    sthEnroll.onmouseout = function (event) {
      event = event || window.event;
      // console.log("移出了");
      sthBox.style.display = "none";
      sth.classList.remove("sth_active");
    };
  }

  // ***5.邮箱验证
  var inpEmail = document.getElementById("email");
  var regEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  var enrollAgain = document.getElementById("enrollAgain");
  check(inpEmail, regEmail);
  // !!!封装检查函数
  function check(inp, regEx) {
    inp.onblur = function (event) {
      event = event || window.event;
      if (regEx.test(this.value)) {
        console.log("邮箱输入正确");
      } else {
        $("#error_email").show().delay(3000).fadeOut();
      }
    };
  }

  // ***6.院校为空验证，否则报名成功
  var btn_submit = document.getElementById("btn_submit");
  btn_submit.onclick = function (event) {
    var $error_school = $("#error_school");
    var $error_emailNull = $("#error_emailNull");
    var $pass = $("#pass");
    var valid = true;
    // console.log(email.value);
    event = event || window.event;
    if (school.innerHTML == "点击选择您所在院校") {
      tips($error_school, 3000);
      valid = false;
    }
    if (email.value == "") {
      tips($error_emailNull, 3000);
      valid = false;
    }
    if (email.value != "" && !regEmail.test(email.value)) {
      $("#error_email").show().delay(3000).fadeOut();
      valid = false;
    }
    if (valid) {
      $pass.text(
        "恭喜您，来自 " +
          $("#school").text() +
          " " +
          $("#year").text() +
          "级 " +
          $("#email").val() +
          " 同学，您的报名信息已记录，请关注您的邮件"
      );
      tips($pass, 5000);
      $("#enrollTextBox").css("display", "none");
      $("#passTextBox").css("display", "block");
    }
  };
  // !!!封装提示函数
  function tips(situation, time) {
    situation.show().delay(time).fadeOut();
  }

  // ***7.重新报名
  enrollAgain.onclick = function (event) {
    event = event || window.event;
    $("#enrollTextBox").css("display", "block");
    $("#passTextBox").css("display", "none");
  };
};
